#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <ostream>
#include <sstream>
#include <string>

#define lld long long // long long 的printf 占位符是lld
#define ENDL '\n'     // 将 endl 替换为 \n 取消缓冲区

const long long MAX_ = 1e9;

using std::cin;
using std::cout;
using std::endl;
using std::string;
const int N = 10000000 + 9;
int ds[N];
int t[N];
int cnt_[N];
void pre_() {
  auto trans = [](int x) {
    int y = x;
    int ans = 0;
    while (x != 0) {
      ans += x % 10;
      x /= 10;
    }
    ds[y] = ans;
  };

  for (size_t i = 2; i < N; ++i)
    trans(i); // cout << ds[i] << ENDL;
  // 判断当前的x是否符合预期

  // check(6);
  for (size_t i = 2; i <= std::sqrt(N); ++i) {
    for (int k = 2; i * k <= N; ++k) {
      if (ds[i] * ds[k] == ds[i * k]) {
        t[i * k] = 1;
      }
    }
  }

  cnt_[1] = 0, t[0] = t[1] = 0;
  for (size_t i = 2; i <= N; ++i)
    t[i] += t[i - 1];
}
void solve() {
  int L = 1, R = 10;
  cin >> L >> R;
  cout << t[R] - t[L - 1] << ENDL;
}
int main() {
  int T = 1;
  pre_();
  cin >> T;

  while (T--) {
    solve();
  }
}